<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    let a = [
        {
            "children": [
                {
                    "children": [
                        {
                            "children": [
                                {
                                    "children": [],
                                    "deptCode": "A02A01A01A01",
                                    "deptName": "徐水保畅养护一班组",
                                    "deptType": 1,
                                    "id": "1797899811841355777",
                                    "parentId": "1797899547222716418",
                                    "sort": 2
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A01A02",
                                    "deptName": "徐水保畅养护二班组",
                                    "deptType": 1,
                                    "id": "1797899858914029570",
                                    "parentId": "1797899547222716418",
                                    "sort": 3
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A01A03",
                                    "deptName": "狼牙山保畅养护一班组",
                                    "deptType": 1,
                                    "id": "1917420198983057410",
                                    "parentId": "1797899547222716418",
                                    "sort": 1
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A01A04",
                                    "deptName": "狼牙山保畅养护二班组",
                                    "deptType": 1,
                                    "id": "1917420282013499394",
                                    "parentId": "1797899547222716418",
                                    "sort": 4
                                }
                            ],
                            "deptCode": "A02A01A01",
                            "deptName": "徐水保通保畅应急大队",
                            "deptType": 1,
                            "id": "1797899547222716418",
                            "parentId": "1797890269220941826",
                            "sort": 1
                        },
                        {
                            "children": [
                                {
                                    "children": [],
                                    "deptCode": "A02A01A02A01",
                                    "deptName": "黄土岭保畅养护一班组",
                                    "deptType": 1,
                                    "id": "1797899917844000770",
                                    "parentId": "1797899607465504770",
                                    "sort": 1
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A02A02",
                                    "deptName": "黄土岭保畅养护二班组",
                                    "deptType": 1,
                                    "id": "1797899984013340674",
                                    "parentId": "1797899607465504770",
                                    "sort": 2
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A02A03",
                                    "deptName": "涞源保畅养护一班组",
                                    "deptType": 1,
                                    "id": "1895370023297077249",
                                    "parentId": "1797899607465504770",
                                    "sort": 3
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A02A04",
                                    "deptName": "涞源保畅养护二班组",
                                    "deptType": 1,
                                    "id": "1917420917605744642",
                                    "parentId": "1797899607465504770",
                                    "sort": 4
                                }
                            ],
                            "deptCode": "A02A01A02",
                            "deptName": "涞源保通保畅应急大队",
                            "deptType": 1,
                            "id": "1797899607465504770",
                            "parentId": "1797890269220941826",
                            "sort": 2
                        },
                        {
                            "children": [
                                {
                                    "children": [],
                                    "deptCode": "A02A01A03A01",
                                    "deptName": "监控一班",
                                    "deptType": 1,
                                    "id": "1917421283445522433",
                                    "parentId": "1797899653711900674",
                                    "sort": 1
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A03A02",
                                    "deptName": "监控二班",
                                    "deptType": 1,
                                    "id": "1917421306602274818",
                                    "parentId": "1797899653711900674",
                                    "sort": 2
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A03A03",
                                    "deptName": "监控三班",
                                    "deptType": 1,
                                    "id": "1917421334997712897",
                                    "parentId": "1797899653711900674",
                                    "sort": 3
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A03A04",
                                    "deptName": "监控四班",
                                    "deptType": 1,
                                    "id": "1917421360440360962",
                                    "parentId": "1797899653711900674",
                                    "sort": 4
                                }
                            ],
                            "deptCode": "A02A01A03",
                            "deptName": "徐水监控中心",
                            "deptType": 1,
                            "id": "1797899653711900674",
                            "parentId": "1797890269220941826",
                            "sort": 3
                        },
                        {
                            "children": [
                                {
                                    "children": [],
                                    "deptCode": "A02A01A04A01",
                                    "deptName": "监控一班",
                                    "deptType": 1,
                                    "id": "1917421381428658178",
                                    "parentId": "1797899688600121345",
                                    "sort": 1
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A04A02",
                                    "deptName": "监控二班",
                                    "deptType": 1,
                                    "id": "1917421401859112962",
                                    "parentId": "1797899688600121345",
                                    "sort": 2
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A04A03",
                                    "deptName": "监控三班",
                                    "deptType": 1,
                                    "id": "1917421432687247361",
                                    "parentId": "1797899688600121345",
                                    "sort": 3
                                },
                                {
                                    "children": [],
                                    "deptCode": "A02A01A04A04",
                                    "deptName": "监控四班",
                                    "deptType": 1,
                                    "id": "1917421458712903682",
                                    "parentId": "1797899688600121345",
                                    "sort": 4
                                }
                            ],
                            "deptCode": "A02A01A04",
                            "deptName": "涞源监控分中心",
                            "deptType": 1,
                            "id": "1797899688600121345",
                            "parentId": "1797890269220941826",
                            "sort": 4
                        }
                    ],
                    "deptCode": "A02A01",
                    "deptName": "指挥调度中心（保通保畅应急中心）",
                    "deptType": 1,
                    "id": "1797890269220941826",
                    "parentId": "1797890183812329473",
                    "sort": 1
                }
            ],
            "deptCode": "A02",
            "deptName": "荣乌分公司",
            "deptType": 1,
            "id": "1797890183812329473",
            "parentId": "0",
            "sort": 2
        }
    ];

    function getSubsetById(obj, targetId, subsetKey = 'children') {
        // 1. 先检查当前对象是否匹配ID
        if (obj.id === targetId) {
            return obj[subsetKey] || []; // 返回子集，无则返回空数组
        }

        // 2. 若当前对象有子集，递归遍历子集
        if (obj[subsetKey] && Array.isArray(obj[subsetKey])) {
            for (const child of obj[subsetKey]) {
                const result = getSubsetById(child, targetId, subsetKey);
                if (result.length > 0) { // 找到结果后直接返回，终止遍历
                    return result;
                }
            }
        }

        // 3. 未找到，返回空数组
        return []
    }

    console.log('==', getSubsetById(a[0], '1797899547222716418'))

    let arr = [{ a: 1 }, { b: 2 }, { a: 1 }]
    let c = arr.map(item => JSON.stringify(item))
    console.log(c);
    let b = new Set(c)
    let d = [...b]
    console.log(d);
    let e = d.map(item => JSON.parse(item))
    console.log(e)
</script>